package com.redsoft.spirit.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.redsoft.spirit.entity.JsonResult;
import com.redsoft.spirit.entity.Page;
import com.redsoft.spirit.service.ComCodeTableService;
import com.redsoft.spirit.service.PageService;

@RestController
public class PageController {

	@Autowired
	private PageService pageService;

	@Autowired
	private ComCodeTableService codeTableService;

	private Gson gson = new Gson();

	/**
	 * 查询系统模块
	 * 
	 * <pre>
	 * 查询系统模块
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@GetMapping("/sys-moudles")
	public String getMoudles() {
		List mouldes = codeTableService.getByParentInnerName("SYS_MOUDLE");
		JsonResult result = new JsonResult(mouldes);
		return result.success().toString();
	}

	/**
	 * 查询所有页面
	 * 
	 * <pre>
	 * 查询所有页面
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@GetMapping("/sys-moudles/{moudleInnerName}/pages")
	public String codeTableTree(@PathVariable String moudleInnerName, int offset, int limit, String searchKey) {
		Map map = pageService.getJsonData(moudleInnerName, offset, limit, searchKey);

		return new JsonResult(map).success().toString();
	}

	/**
	 * 保存页面
	 * 
	 * <pre>
	 * 保存页面（新增或者修改的保存）
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/pages")
	public String save(@RequestBody Page page) throws Exception {
		pageService.save(page);
		return new JsonResult().success().toString();
	}

	/**
	 * 删除此项以及所有子项
	 * 
	 * <pre>
	 * 删除此项以及所有子项
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 */
	@DeleteMapping("/pages/{id}")
	public String delPage(@PathVariable String id) {
		pageService.deleteAllById(id);
		return new JsonResult().success().toString();
	}
}